package com.chuqiyun.authorization.cron;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chuqiyun.authorization.entity.Product;
import com.chuqiyun.authorization.service.ProductService;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

/**
 * @author mryunqi
 * @date 2023/5/20
 */
@Component
@EnableScheduling
public class ProductCron {
    @Resource
    private ProductService productService;

    @Async
    @Scheduled(fixedDelay = 1000*60*15)  //每隔15分钟执行一次
    public void productCron() throws IOException{
        QueryWrapper<Product> productQueryWrapper = new QueryWrapper<>();
        productQueryWrapper.eq("state",0);
        List<Product> productList = productService.list(productQueryWrapper);
        for (Product product: productList) {
            long nowTime = System.currentTimeMillis()/1000;
            if (nowTime > product.getExpirationDate()){
                product.setState(1);
                productService.updateById(product);
            }
        }
    }
}
